<?php

/**
 *  
 */
class AdminBlogArticleController extends AdminBaseController
{
    public $blog = null;
    
    /**
     *  before ...
     */
    public function beforeAction($action)
    {
        parent::beforeAction($action);

        $blog = $this->serviceManager->getFocusBlog();
        if( !$blog ) {
            Request::redirect('admin/');
        }

        $this->blog = $blog;
        $this->menus->setFocusByAction('adminBlogArticle/list');
        return true;
    }

    /**
     *  index
     */
    public function actionIndex()
    {
        Request::redirect('list');
    }

    /**
     *  list
     */
    public function actionList( $page=1, $searchKey='' )
    {
        $blogArticles = new BlogArticles();
        $myBlogArticles = $blogArticles->getBlogArticles( BlogArticle::STATUS_ALL, $page );
        $rowCount       = $blogArticles->getNumBlogArticles( BlogArticle::STATUS_ALL );

        $this->pageLimit = new PageLimitManager();
        $this->pageLimit->setBaseUrl( $this->createUrl('') );
        $this->pageLimit->setRowCount( $rowCount );
        $this->pageLimit->setPage( $page );
        $this->pageLimit->setSearchKey( $searchKey );

        $this->render('list',array(
            'blogArticles' => $myBlogArticles, 
        ));
    }

    /**
     *  new
     */
    public function actionNew()
    {
        $this->layout='//admin/admin_layout_simple';

        // add 
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();

            $blogArticle = new BlogArticle();
            $blogArticle->setUserId     ( $this->user->getId() );
            $blogArticle->setBlogId     ( $this->blog->getId() );
            $blogArticle->setStatus     ( $posts['status'] );
            $blogArticle->setCreateDate ( time() );
            $blogArticle->setUpdateDate ( time() );

            $blogArticleText = new BlogArticleText();
            $blogArticleText->setUserId    ( $this->user->getId() );
            $blogArticleText->setBlogId    ( $this->blog->getId() );
            $blogArticleText->setArticleId ( 0 );
            $blogArticleText->setTopic     ( $posts['topic'] );
            $blogArticleText->setText      ( $posts['text'] );

            // validate
            $messages = $blogArticleText->validate();
            $blogArticleText->filter();
            if( $messages ) {
                $this->sessionManager->setErrorMessages('新增失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticle'] = $blogArticle;
                $data['blogArticleText'] = $blogArticleText;
                $this->render('new', $data );
                return;
            }

            // validate
            $messages = $blogArticle->validate();
            $blogArticle->filter();
            if( $messages ) {
                $this->sessionManager->setErrorMessages('新增失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticle'] = $blogArticle;
                $data['blogArticleText'] = $blogArticleText;
                $this->render('new', $data );
                return;
            }

            // add
            $blogArticles = new BlogArticles();
            $blogArticleId = $blogArticles->addBlogArticle($blogArticle);

            $blogArticle = $blogArticles->getBlogArticle($blogArticleId);
            if( !$blogArticle ) {
                $this->sessionManager->setErrorMessages('新增失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticle'] = $blogArticle;
                $data['blogArticleText'] = $blogArticleText;
                $this->render('new', $data );
                return;
            }

            $blogArticleTexts = new BlogArticleTexts();
            $blogArticleText->setArticleId( $blogArticleId );
            $blogArticleTexts->addBlogArticleText( $blogArticleText );

            $this->sessionManager->setSuccessMessages('新增成功');

        }

        // new
        $data['blogArticle']     = new BlogArticle();
        $data['blogArticleText'] = new BlogArticleText();
        $this->render( 'new', $data );

    }


    /**
     *  edit
     */
    public function actionEdit( $id )
    {
        $this->layout='//admin/admin_layout_simple';

        $id = (int) $id;
        $blogArticles = new BlogArticles();
        $blogArticle = $blogArticles->getBlogArticle( $id, $this->blog->getId() );
        if(!$blogArticle) {
            die('not find article');
        }

        // update 
        if( Request::isPost() ) {
            $posts = Request::getPostInfo();

            // 注意, 不會修改原有的 user id , blog id

            $blogArticle->setStatus     ( $posts['status'] );
            $blogArticle->setUpdateDate ( time() );

            $blogArticleText = $blogArticle->getArticleText();
            $blogArticleText->setTopic ( $posts['topic'] );
            $blogArticleText->setText  ( $posts['text'] );

            // validate
            $messages = $blogArticleText->validate();
            $blogArticleText->filter();
            if( $messages ) {
                $this->sessionManager->setErrorMessages('修改失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticle'] = $blogArticle;
                $data['blogArticleText'] = $blogArticleText;
                $this->render('edit', $data );
                return;
            }

            // validate
            $messages = $blogArticle->validate();
            $blogArticle->filter();
            if( $messages ) {
                $this->sessionManager->setErrorMessages('修改失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticle'] = $blogArticle;
                $data['blogArticleText'] = $blogArticleText;
                $this->render('edit', $data );
                return;
            }

            // update
            $result = $blogArticles->updateBlogArticle($blogArticle);
            if( !$result ) {
                $this->sessionManager->setErrorMessages('修改失敗');
                $data['fieldsMessages'] = $messages;
                $data['blogArticle'] = $blogArticle;
                $data['blogArticleText'] = $blogArticleText;
                $this->render('new', $data );
                return;
            }

            $blogArticleTexts = new BlogArticleTexts();
            $blogArticleTexts->updateBlogArticleText( $blogArticleText );

            $this->sessionManager->setSuccessMessages('修改成功');

        }


        //edit
        $data['blogArticle']     = $blogArticle;
        $data['blogArticleText'] = $blogArticle->getArticleText();
        $this->render( 'edit', $data );

    }

}
